/*
 * Copyright 2010 Gal Dolber.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.guit.demo.client.main;

import com.google.gwt.dom.client.Style.Visibility;
import com.google.gwt.user.client.ui.Widget;
import com.google.inject.Inject;

import com.guit.client.GuitPresenter;
import com.guit.client.View;
import com.guit.client.binder.GuitBinder;
import com.guit.client.binder.ViewField;
import com.guit.client.binder.ViewHandler;
import com.guit.client.binder.ViewInitializer;
import com.guit.demo.client.attacher.RootLayoutPanelAttacher;
import com.guit.demo.client.blippanel.BlipPanel;
import com.guit.demo.client.common.HasInnerHTML;
import com.guit.demo.client.common.HasWidget;
import com.guit.demo.client.complexplace.ComplexPlaceData;
import com.guit.demo.client.complexplace.ComplexPlacePresenter;
import com.guit.demo.client.dynamic.DynamicPresenter;
import com.guit.demo.client.jsonserializer.JsonSerializerPresenter;
import com.guit.demo.client.login.LoginPresenter;
import com.guit.demo.client.main.MainPresenterImpl.MainBinder;
import com.guit.demo.client.microevents.MicroeventsPresenter;
import com.guit.demo.client.styler.Styler;

import java.util.LinkedHashMap;
import java.util.Map.Entry;

public class MainPresenterImpl extends GuitPresenter<MainBinder> implements MainPresenter {

  public interface MainBinder extends GuitBinder<MainPresenterImpl> {
  }

  private final RootLayoutPanelAttacher attacher;

  @ViewField
  public HasInnerHTML navegation;

  @ViewField
  public HasWidget panel;

  @ViewField
  Styler loading;

  LinkedHashMap<String, String> places = new LinkedHashMap<String, String>();

  @Inject
  public MainPresenterImpl(RootLayoutPanelAttacher attacher) {
    this.attacher = attacher;
  }

  @ViewHandler
  public void back$click() {
    placeManager.goBack();
  }

  @ViewHandler
  public void goDefault$click() {
    placeManager.goDefault();
  }

  @ViewHandler
  public void forward$click() {
    placeManager.goForward();
  }

  @Override
  protected void initialize() {
    attacher.attach(getView());
  }

  @ViewInitializer
  protected void initializeView() {
    places.put("Login", placeManager.getToken(LoginPresenter.class));
    places.put("Dynamic", placeManager.getToken(DynamicPresenter.class));
    places.put("Complex blue", placeManager.getToken(ComplexPlacePresenter.class,
        new ComplexPlaceData("Hello", "blue")));
    places.put("Complex red", placeManager.getToken(ComplexPlacePresenter.class,
        new ComplexPlaceData("Goodbye", "red")));
    places.put("Microevents", placeManager.getToken(MicroeventsPresenter.class));
    places.put("Json Serializer", placeManager.getToken(JsonSerializerPresenter.class));
    places.put("Blips", placeManager.getToken(BlipPanel.class));

    loading.setVisibility(Visibility.HIDDEN);
  }

  private void render(int selected) {
    StringBuilder sb = new StringBuilder();
    int index = 0;
    for (Entry<String, String> entry : places.entrySet()) {
      sb.append("<a href='#");
      sb.append(entry.getValue());
      sb.append("'");
      if (selected == index) {
        sb.append(" style='font-weight:bold'");
      }
      sb.append(">");
      sb.append(entry.getKey());
      sb.append("</a><br/>");
      index++;
    }
    navegation.setInnerHTML(sb.toString());
  }

  @Override
  public void setContent(View content, int selected) {
    panel.setWidget((Widget) content);
    render(selected);
  }
}
